/*******************************************************************************
 **  Name: db.cpp                                                             **
 **  Description:                                                             **
 **                                                                           **
 **  Open Source Initiative (OSI) Approved License                            **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  The contents of this file are subject to the terms of the CDDL:          **
 **  Common Development and Distribution License (the "License").             **
 **  You may not use this file except in compliance with the License.         **
 **                                                                           **
 **  You can obtain a copy of the license at $PROJECT_ROOT/LICENSE            **
 **  or http://www.opensolaris.org/os/licensing.  This code is Open Source    **
 **  and you are free to use it within the confines of the license, even      **
 **  for your own closed-source commercial projects, as long as you follow    **
 **  the terms set forth in the CDDL.                                         **
 **                                                                           **
 **  When distributing Covered Code, include this CDDL HEADER in each         **
 **  file and include the License file at $PROJECT_ROOT/LICENSE.              **
 **  If applicable, add the following below this CDDL HEADER, with the        **
 **  fields enclosed by brackets "[]" replaced with your own identifying      **
 **  information: Portions Copyright [yyyy] [name of copyright owner]         **
 **                                                                           **
 ******************************************************************************/
// Modified $Date: 2010-03-16 22:12:41 -0600 (Tue, 16 Mar 2010) $ by $Author: bjames $
#define db_cpp_ID    "$Id: db.cpp 131 2010-03-17 04:12:41Z bjames $"

#include <string>
#include <vector>

#include "db.h"
#include "log.h"


#if defined(__MYSQLPP__)
///////////////////////////////[ MySQLDB
/// We only load help tutorial and Agency HQ in memory at the moment. All 
/// other areas are pulled from the DB as they are visited.
//mysqlpp::StoreQueryResult
std::vector<MySQLArea>
MySQLDB::LoadAreas(int setting)
{
    DBGFUNC();

    std::vector<MySQLArea>   v;
    if (bInitialized == false) return v;

    char buff[256];
    std::string     qry(qAreas);
    sprintf(buff,qry.c_str(),setting);

    mysqlpp::Query query = conn.query(buff);
    query.storein(v);

    return v;
}

int
MySQLDB::LoadNPCs()
{
    DBGFUNC();

    return 0;
}

mysqlpp::StoreQueryResult
MySQLDB::Query(const char * q)
{
    DBGFUNC();
    mysqlpp::Query query  = conn.query(q);
    mysqlpp::StoreQueryResult res = query.store();

    return res;
}

bool
MySQLDB::Init()
{
    DBGFUNC();

    debug::log->out(LOG_DEBUG,"mysql++.connect(%s,%s,%s,%s)",
             db.c_str(),host.c_str(),dbuser.c_str(),dbpass.c_str());
    if (conn.connect(db.c_str(),host.c_str(),dbuser.c_str(),dbpass.c_str()))
    {
        bInitialized = true;
        return true;
    }

    return false;
}

MySQLDB::MySQLDB(std::string s)
    : DataBase(s)
{
    
}
#endif


///////////////////////////////[ ServerCfg
int
ServerCfg::Parse()
{
    DBGFUNC();

    char buff[256];
    char a[64], v[64];
    //std::string buff;

    while (!conf.eof()) {
        conf.getline(buff,256);
        if (buff[0] == '#' || buff[0] == '\n' || buff[0] == '\r' || buff[0] == '\0')
            continue;

        sscanf(buff,"%s = %s",a,v);
        debug::log->out(LOG_DEBUG,"Adding %s = %s",a,v);
        avpairs[a] = v;
    }

    return avpairs.size();
}

bool
ServerCfg::Init()
{
    DBGFUNC();

    conf.open(db);
    if (conf.good()) {
        bInitialized = true;
        return true;
    }

    return false;
}

ServerCfg::ServerCfg(std::string s)
    : DataBase(s)
{
    debug::log->out(LOG_DEBUG,"Using conf: %s",s.c_str());
}

///////////////////////////////[ DataBase

//constructor
DataBase::DataBase(std::string s)
{
    db = s;
    bInitialized = false;
}

//destructor
DataBase::~DataBase(void)
{
}
